package com.cardone.platform.common.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.EntityExtendDto;

/**
 * 实体扩展
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class EntityExtendJdbcDao implements EntityExtendDao {
	@Override
	public int deleteByIds(final EntityExtendDto deleteEntityExtend) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteEntityExtend.getAttrs())) {
			model.putAll(deleteEntityExtend.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteEntityExtend.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityExtendDto findEntityExtend) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityExtend, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityExtend, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findEntityExtend.getAttrs())) {
			model.putAll(findEntityExtend.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityExtendDto findEntityExtend) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityExtend);

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityExtend, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findEntityExtend.getAttrs())) {
			model.putAll(findEntityExtend.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityExtendDto findListEntityExtend) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListEntityExtend).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListEntityExtend.getAttrs())) {
			model.putAll(findListEntityExtend.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final EntityExtendDto insertEntityExtend) {
		if (StringUtils.isBlank(insertEntityExtend.getId())) {
			insertEntityExtend.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertEntityExtend));

		if (MapUtils.isNotEmpty(insertEntityExtend.getAttrs())) {
			model.putAll(insertEntityExtend.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityExtendDto paginationEntityExtend) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationEntityExtend).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationEntityExtend.getAttrs())) {
			model.putAll(paginationEntityExtend.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationEntityExtend), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityExtendDto readEntityExtend) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readEntityExtend, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readEntityExtend.getAttrs())) {
			model.putAll(readEntityExtend.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readEntityExtend);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readEntityExtend, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityExtendDto saveEntityExtend) {
		final P oldEntityExtend = this.findByCodeOrId(mappedClass, saveEntityExtend);

		if (oldEntityExtend == null) {
			this.insertByCode(saveEntityExtend);

			return this.findByCodeOrId(mappedClass, saveEntityExtend);
		}

		final int updateCount = this.updateByCode(saveEntityExtend, oldEntityExtend);

		if (updateCount < 1) {
			return oldEntityExtend;
		}

		return this.findByCodeOrId(mappedClass, saveEntityExtend);
	}

	@Override
	public int updateByCode(final EntityExtendDto updateEntityExtend) {
		final EntityExtendDto oldEntityExtend = this.findByCodeOrId(EntityExtendDto.class, updateEntityExtend);

		return this.updateByCode(updateEntityExtend, oldEntityExtend);
	}

	private <P> int updateByCode(final EntityExtendDto updateEntityExtend, final P oldEntityExtend) {
		if (oldEntityExtend == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateEntityExtend);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateEntityExtend, oldEntityExtend, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldEntityExtend, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldEntityExtend, updateEntityExtend, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateEntityExtend, updateProperties), new ModelArgs(updateEntityExtend, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateEntityExtend.getAttrs())) {
			model.putAll(updateEntityExtend.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}